package com.hejjon.util;

import com.hejjon.core.exception.BusinessException;
import com.hejjon.enumer.DriverEnum;
import org.apache.commons.lang3.ArrayUtils;
import org.apache.commons.lang3.StringUtils;

import java.util.Locale;

public class DBUtil {

    /**
     * 拼接连接URL字符串
     * @param host
     * @param port
     * @param driverType
     * @return
     */
    public static String getUrlByInfo(String host, Long port, String driverType) {
        DriverEnum driverEnum = DriverEnum.getInstance(driverType);
        if (driverEnum == null) {
            throw new BusinessException("未知的驱动类型: " + driverType);
        }
        switch (driverEnum) {
            case MYSQL:
                return "jdbc:mysql://" + host + ":" + port + "/";
            case ORACLE:
                return "jdbc:oracle:thin:@" + host + ":" + port + ":orcl";
            case SQL_SERVER:
                return "jdbc:sqlserver://" + host + ":" + port + ";";
        }
        return "";
    }

    /**
     * 不需要设置长度的字段类型
     * 日期 日期时间 时间戳类型
     * @param colType
     * @return
     */
    public static boolean noNeedLength(String colType) {
        if (StringUtils.isBlank(colType)) {
            return false;
        }
        colType = colType.toLowerCase(Locale.ROOT);
        String[] types = new String[] {"date", "datetime", "timestamp", "longtext", "double"};
        return ArrayUtils.contains(types, colType);
    }

    /**
     * 不能设置默认值的字段类型
     * @param colType
     * @return
     */
    public static boolean cannotSetDefaultValue(String colType) {
        if (colType == null || "".equals(colType)) {
            return false;
        }
        colType = colType.toLowerCase(Locale.ROOT);
        String[] types = new String[] {"text", "longtext", "blob", "json"};
        return ArrayUtils.contains(types, colType);
    }

    /**
     * 获取完整表名称
     *
     */
    public static String getFullTableName(String dataBaseName, String tableName) {
        return dataBaseName + "." + tableName;
    }






}
